package com.itlong.cloud.sdk.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.itlong.cloud.POJO.PO.ProjectPO;
import com.itlong.cloud.POJO.PO.PropertyRoleProjectPO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.constants.RabbitMQConstant;
import com.itlong.cloud.enumerate.DataStatusEnum;
import com.itlong.cloud.enumerate.PropertyPublicRoleEnum;
import com.itlong.cloud.rabbitmq.config.MessageSender;
import com.itlong.cloud.sdk.dao.ISDKServerOldProjectDao;
import com.itlong.cloud.sdk.service.ISDKServerOldProjectService;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.node.JsonUtil;
import com.itlong.cloud.utils.random.LogicIdUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 *      SDK地址服务层接口实现类
 * </desc>
 *
 * @createDate 2017/10/31
 */
@Service
public class SDKServerOldProjectServiceImpl implements ISDKServerOldProjectService {

    @Autowired
    private ISDKServerOldProjectDao iSDKProjectDao;

    @Autowired
    private MessageSender messageSender;


    /**
     * <desc>
     *      根据自定义参数查询项目
     * <desc/>
     * @param  params   查询参数集
     * @return  List<ProjectPO>  返回PO对象集
     * @author Biao.R
     * @createDate 2017/10/31
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public List<ProjectPO> getByParamMap(Map<String, Object> params) {
        return iSDKProjectDao.getByParamMap(params);
    }

    /**
     * <desc>
     *      查找以beginStr开头的最大项目编号
     * <desc/>
     * @param   beginStr 查询的起始字符串
     * @return  String  返回的项目编号
     * @author Biao.R
     * @createDate 2017/10/31
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Integer getMaxId(String beginStr) {
        return iSDKProjectDao.getMaxId(beginStr);
    }


    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public ProjectPO saveByProjectName(String appid, String projectName) throws Exception {
        Date currentTime = new Date();
        //查询以03开头项目编号的最大值
        Integer maxId = iSDKProjectDao.getMaxId("03");
        String projectId = String.format("%08d", maxId + 1);
        ProjectPO projectPO = new ProjectPO(projectId, projectName, "sdk端添加", LogicIdUtil.getProjectKey(), 10);
        projectPO.setCreateTime(currentTime);
        projectPO.setStatus(DataStatusEnum.NORMAL.getType());
        //绑定当前appId为该项目的独立开发者
        PropertyRoleProjectPO roleProjectPO = new PropertyRoleProjectPO(projectId, PropertyPublicRoleEnum.DEVELOPER.getRoleId(), appid);
        //持久化数据
        Integer projectCount = iSDKProjectDao.save(SqlUtil.durableData(projectPO, PlatformConstants.TABLE_SAVE));
        JSONObject js = new JSONObject();
        js.put("data",roleProjectPO);
        messageSender.send(RabbitMQConstant.RAABITMQ_SDK_SAVEDEVELOPER_QUENUE, JsonUtil.toJSON(js), RabbitMQConstant.RABBITMQ_USER_EXCHANGE,
                RabbitMQConstant.RAABITMQ_SDK_SAVEDEVELOPER_KEY);
        return projectPO;
    }

    /**
     * <desc>
     *      查询独立开发者所管辖的项目信息
     * <desc/>
     * @param   propertyRoleProjectPO 独立开发者PO对象
     * @return  List<ProjectPO> 查询出的项目信息结果集
     * @author Biao.R
     * @createDate 2017/11/3
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public List<ProjectPO> getDeveloperProjects(PropertyRoleProjectPO propertyRoleProjectPO) {
        Map<String,Object> params = new HashMap<>();
        params.put("userId",propertyRoleProjectPO.getUserId());
        params.put("roleId",propertyRoleProjectPO.getRoleId());
        params.put("projectId",propertyRoleProjectPO.getProjectId());
        return iSDKProjectDao.getDeveloperProjects(params);
    }

    /**
     * <desc>
     *      根据项目ID查询单个项目
     * <desc/>
     * @param  projectId  待查询项目ID
     * @return  ProjectPO  返回对象
     * @author Biao.R
     * @createDate 2017/11/9
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public ProjectPO getByProjectId(String projectId) {
        if(StringUtils.isEmpty(projectId)){
            return null;
        }
        return iSDKProjectDao.getByProjectId(projectId);
    }

}